package com.haojin.spring.resources;

import org.springframework.core.io.UrlResource;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author .29.
 * @create 2023-03-01 9:11
 */
public class urlResources {
    public static void loadAndReadUrlResources(String path){
        UrlResource url = null;

        try {
            url = new UrlResource(path);
            //获取资源名
            System.out.println(url.getURL());
            System.out.println(url.getFilename());
            //获取资源描述
            System.out.println(url.getDescription());
            //获取资源内容
            System.out.println(url.getInputStream().read());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        //访问网络资源，测试UrlResource功能
//        loadAndReadUrlResources("http://www.baidu.com");

        //方法二：读取文件获取路径
        loadAndReadUrlResources("file:baidu.txt");
    }
}
